const { toUrl,globalData,msgLoading } = getApp()
import { request } from '@/utils/request'
Page({
    toUrl,
    data: {
        globalData,
        menu: 0,
        type: 'normal',
        list: [
            { select: true },
            { select: false }
        ],
        page: 1,
        count: 1,
        typeObj: null,
        coupons: null,
        loading:true,
        use: [], //可用优惠券
        notUse: null, //不可用优惠券
        money: 0,
        couponsId: 0,
        isconfirm: false, //点击确认返回
        isBack: false //点击返回按钮
    },
    goBack() {
        this.setData({
            isBack: true  
        })
        //购物车返回
        if(this.data.typeObj.type == 'cart') {
            
            wx.redirectTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&suppliers_id='+this.data.typeObj.suppliers_id+'&ids='+this.data.typeObj.ids,
            })
        }
        //直接购买返回
        if(this.data.typeObj.type == 'buy') {
            // console.log('/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&goods_id='+this.data.typeObj.goods_id+'&item_id='+this.data.typeObj.item_id+'&num='+this.data.typeObj.num)
            wx.redirectTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&goods_id='+this.data.typeObj.goods_id+'&item_id='+this.data.typeObj.item_id+'&num='+this.data.typeObj.num
            })
        }
        //复购返回
        if(this.data.typeObj.type == 'repurchase') {
            wx.navigateTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&order_id='+this.data.typeObj.order_id
            })
        }
    },
    select (e) {
        const { index } = e.currentTarget.dataset
        const { select } = this.data.use[index]
        this.data.use.map(item=>{ item.select = false })
        this.data.use[index].select = !select
        if(this.data.use[index].select) {
            this.setData({
                money: this.data.use[index].money * 1,
                couponsId: this.data.use[index].coupon_id
            })
        }else {
            this.setData({
                money: 0,
                couponsId: 0
            })
        }
        // console.log(this.data.use[index])
        this.setData({
            use: this.data.use
        })
    },
    /*
    * 获取优惠券
    * 购物车和直接购买
    */
    getTicket(data) {
        const that = this
        request('cart/addorder',{data}).then(res => {
            const list = res.data.coupons
            const use = []
            const notUse = []
            list.forEach(item => {
                if(this.data.typeObj.coupons_id) {
                    if(this.data.typeObj.coupons_id == item.coupon_id) {
                        item.select = true
                        this.setData({
                            couponsId: item.coupon_id,
                            money: item.money * 1
                        })
                    }else {
                        item.select = false
                    }
                }else {
                    item.select = false
                }
                
                if(item.is_use == 1) {
                    use.push(item)
                }else {
                    notUse.push(item)
                }
            })
            that.setData({
                use: use.length > 0 ? use : null,
                notUse: notUse.length > 0 ? notUse : null,
                coupons: res.data.coupons.length > 0 ? res.data.coupons : null,
                loading:false
            })
        }).finally(()=>{
            wx.hideLoading()
        })
    },
    //确认
    confirmCoupon() {
        this.setData({
            isconfirm: true
        })
        console.log(this.data.typeObj)
        //购物车返回
        if(this.data.typeObj.type == 'cart') {
            
            wx.redirectTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&suppliers_id='+this.data.typeObj.suppliers_id+'&ids='+this.data.typeObj.ids,
            })
        }
        //直接购买返回
        if(this.data.typeObj.type == 'buy') {
            // console.log('/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&goods_id='+this.data.typeObj.goods_id+'&item_id='+this.data.typeObj.item_id+'&num='+this.data.typeObj.num)
            wx.redirectTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&goods_id='+this.data.typeObj.goods_id+'&item_id='+this.data.typeObj.item_id+'&num='+this.data.typeObj.num
            })
        }
        //复购返回
        if(this.data.typeObj.type == 'repurchase') {
            wx.navigateTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&order_id='+this.data.typeObj.order_id
            })
        }
        
    },
    onLoad({ type,obj }) {
        msgLoading()
        console.log(type,obj);
        if(type) {
            this.setData({
                type
            })
        }
        const subObj = JSON.parse(obj)
        this.setData({
            typeObj: subObj
        })
        if(subObj.type == 'cart') {
            const data = {
                typeid: 2,
                coupon_id: 0,
                ids: this.data.typeObj.ids,
                suppliers_id: this.data.typeObj.suppliers_id
            }
            this.getTicket(data)
        }
        if(subObj.type == 'buy') {
            const data = {
                typeid: 1,
                coupon_id: 0,
                goods_id: this.data.typeObj.goods_id,
                num: this.data.typeObj.num
            }
            if(this.data.typeObj.item_id) {
                data.item_id = this.data.typeObj.item_id
            }
            this.getTicket(data)
        }
        if (subObj.type == 'repurchase') {
            const data = {
                order_id: this.data.typeObj.order_id,
                coupon_id: 0,
            }
            this.repeatgetTicket(data)
        }
    },
    repeatgetTicket(data) {
        const that = this
        request('cart/againorder',{data}).then(res => {
            const list = res.data.coupons
            const use = []
            const notUse = []
            list.forEach(item => {
                item.select = false
                if(item.is_use == 1) {
                    use.push(item)
                }else {
                    notUse.push(item)
                }
            })
            that.setData({
                use: use.length > 0 ? use : null,
                notUse: notUse.length > 0 ? notUse : null,
                coupons: res.data.coupons.length > 0 ? res.data.coupons : null,
                loading:false
            })
        }).finally(()=>{
            wx.hideLoading()
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        const page = getCurrentPages()
        console.log(page)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        if(!this.data.isconfirm && !this.data.isBack) {
            //购物车返回
        if(this.data.typeObj.type == 'cart') {
            
            wx.navigateTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&suppliers_id='+this.data.typeObj.suppliers_id+'&ids='+this.data.typeObj.ids,
            })
        }
        //直接购买返回
        if(this.data.typeObj.type == 'buy') {
            // console.log('/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&goods_id='+this.data.typeObj.goods_id+'&item_id='+this.data.typeObj.item_id+'&num='+this.data.typeObj.num)
            wx.navigateTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&goods_id='+this.data.typeObj.goods_id+'&item_id='+this.data.typeObj.item_id+'&num='+this.data.typeObj.num
            })
        }
        //复购返回
        if(this.data.typeObj.type == 'repurchase') {
            wx.navigateTo({
                url: '/pages/order/form?coupons_id='+this.data.couponsId+'&type='+this.data.typeObj.type+'&order_id='+this.data.typeObj.order_id
            })
        }
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})